Skip to content

feat(auth): Introduce Regional Access Boundaries for GoogleCredentials#12787

Open
vverman wants to merge 5 commits intogoogleapis:regional-access-boundariesfrom
vverman:rab-migration
Open

feat(auth): Introduce Regional Access Boundaries for GoogleCredentials#12787
vverman wants to merge 5 commits intogoogleapis:regional-access-boundariesfrom
vverman:rab-migration

Conversation

@vverman
Copy link
Copy Markdown
Contributor

@vverman vverman commented Apr 13, 2026

@vverman vverman requested review from a team as code owners April 13, 2026 23:24
@vverman vverman changed the base branch from main to regional-access-boundaries April 13, 2026 23:25
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for Regional Access Boundaries (RAB) across various credential types, including Compute Engine, Service Account, and External Account credentials. It adds a RegionalAccessBoundaryManager to handle the lifecycle, caching, and asynchronous refreshing of RAB information, which is then included in outgoing API requests via the x-allowed-locations header. Feedback includes a recommendation to use try-with-resources for HttpResponse to prevent connection leaks, a suggestion to maintain map immutability in getAdditionalHeaders to avoid regressions, and a request to use a shared, bounded ExecutorService instead of creating new threads for background refreshes.

Comment on lines +252 to +261
RegionalAccessBoundaryResponse json;
try {
HttpResponse response = request.execute();
String responseString = response.parseAsString();
JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser(responseString);
json = parser.parseAndClose(RegionalAccessBoundaryResponse.class);
} catch (IOException e) {
throw new IOException(
"RegionalAccessBoundary: Failure while getting regional access boundaries:", e);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The HttpResponse object is not closed, which can lead to a connection leak. It is recommended to use a try-with-resources block to ensure the response is closed properly. Additionally, the parsing logic can be simplified by using response.parseAs(RegionalAccessBoundaryResponse.class).

    RegionalAccessBoundaryResponse json;
    try (HttpResponse response = request.execute()) {
      json = response.parseAs(RegionalAccessBoundaryResponse.class);
    } catch (IOException e) {
      throw new IOException(
          "RegionalAccessBoundary: Failure while getting regional access boundaries:", e);
    }
References
  1. When managing a collection of closeable resources (e.g., scopes), ensure they are closed in the reverse order of their creation (LIFO). The implementation must be exception-safe to prevent resource leaks, meaning all opened resources should be closed even if exceptions occur during their creation or closing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do a quick follow up PR which addresses this and other points if that's okay with you?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds good.

Comment on lines 582 to 588
@Override
protected Map<String, List<String>> getAdditionalHeaders() {
Map<String, List<String>> headers = super.getAdditionalHeaders();
Map<String, List<String>> headers = new HashMap<>(super.getAdditionalHeaders());

String quotaProjectId = this.getQuotaProjectId();
if (quotaProjectId != null) {
return addQuotaProjectIdToRequestMetadata(quotaProjectId, headers);
}
return headers;
return addQuotaProjectIdToRequestMetadata(quotaProjectId, headers);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of getAdditionalHeaders creates a mutable HashMap and returns it if quotaProjectId is null, which is a regression in safety from the previous implementation that always returned an unmodifiable map. This can be simplified by passing the result of super.getAdditionalHeaders() directly to addQuotaProjectIdToRequestMetadata, which already handles null project IDs correctly.

  @Override
  protected Map<String, List<String>> getAdditionalHeaders() {
    return addQuotaProjectIdToRequestMetadata(getQuotaProjectId(), super.getAdditionalHeaders());
  }

Comment on lines +169 to +171
Thread refreshThread = new Thread(refreshTask, "RAB-refresh-thread");
refreshThread.setDaemon(true);
refreshThread.start();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Creating a new Thread for every background refresh is generally discouraged in library code as it can lead to excessive thread creation if many credential instances are used. It is better to use a shared, bounded ExecutorService to manage background tasks more efficiently.

References
  1. For safety, use a bounded thread pool (e.g., ThreadPoolExecutor with a defined maximum size) instead of an unbounded one (e.g., Executors.newCachedThreadPool()), even if the current logic seems to limit concurrent tasks.

vverman added 3 commits April 13, 2026 17:19
# Conflicts:
#	google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/ExternalAccountCredentialsTest.java
#	google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/GoogleCredentialsTest.java
@lqiu96 lqiu96 changed the title chore: Migrate Regional Access Boundaries. feat(auth): Introduce Regional Access Boundaries for GoogleCredentials Apr 14, 2026
*/
package com.google.cloud.dataplex.v1;


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this an unintended change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is a linter change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd. Do you have an error message that required this change? Or was it a command you ran that ended up making this change?

Copy link
Copy Markdown
Member

@lqiu96 lqiu96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR was approved in the auth library: googleapis/google-auth-library-java#1880

@vverman vverman requested a review from nbayati April 14, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants